package org.springframework.web.servlet;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

//@WebServlet("/DispatcherServlet")
public class DispatcherServlet extends HttpServlet {
    //用来存放工厂创建的对象
    static Map<String,Method> map=new HashMap<>();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取用户的请求路劲
        String uri=request.getRequestURI();
        String path=uri.substring(request.getContextPath().length());
        System.out.println("请求过来的path:"+path);
        ///dev/HelloServlet
        //2.根据请求路劲寻找要执行的方法
        Method method=map.get(path);
        //3.执行方法
        //获取方法所在类的对象
        Class clz=method.getDeclaringClass();
        try {
            //创建方法所在类的对象
            Object o=clz.newInstance();
            //获取方法参数列表
            Class[] pa=method.getParameterTypes();

            if(pa.length==0){
                method.invoke(o);
            }else if(pa.length==1){
                method.invoke(o,request);
            }else if(pa.length==2){
                method.invoke(o,request,response);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        try {
            //扫包
            String packee="cn.bdqn.controller";
            //获取项目中classes的路劲
            String path=this.getClass().getClassLoader().
                    getResource("/").getPath();
            path=path+(packee.replace(".","/"));
            //根据路劲创建文件对象
            File file=new File(path.substring(1));
            //遍历文件夹下面所有的文件
            File[] files = file.listFiles();
            if(files!=null&&files.length>0){
                for(File file2:file.listFiles()){
                    //获取文件名 类名.class  UserController.class
                    String fileName= file2.getName();

                    //创建该文件所对应类的对象
                    //cn.bdqn.controller.UserController
                    Class clz=Class.forName(packee+"."+
                            fileName.substring(0,fileName.indexOf(".")));
                    //获取该类中所有的方法
                    Method[] methods=clz.getDeclaredMethods();
                    //遍历数组
                    for (Method method : methods) {
                        if(method.isAnnotationPresent(RequestMapping.class)){
                            //获取注解对象
                            RequestMapping requestMapping=(RequestMapping)method.
                                    getAnnotation(RequestMapping.class);
                            map.put(requestMapping.value(),method);
                            System.out.println("存入的map中的数据："+requestMapping.value());
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
